﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace HoloJson.MiniDriver
{
    public sealed partial class ParserDemoControl : UserControl
    {
        // temporary
        private const string jsonFileOpenPickerSettingsIdentifier = "jsonFileOpenPicker";

        // temporary
        private PickerLocationId defaultJSONFileFolderId;

        // Currently selected file.
        private StorageFile currentJSONFile = null;

        private readonly ParserDemoDriver demoDriver = new ParserDemoDriver();

        public ParserDemoControl()
        {
            this.InitializeComponent();

            // temporary
            defaultJSONFileFolderId = PickerLocationId.ComputerFolder;

            // temporary
            ButtonParse.IsEnabled = false;

            // temporary
            ButtonParse.Background = new SolidColorBrush(Colors.Navy);
            // ...
        }

        private async void ButtonSelect_Click(object sender, RoutedEventArgs e)
        {
            currentJSONFile = await ChooseInputFile(jsonFileOpenPickerSettingsIdentifier, defaultJSONFileFolderId);
            if (currentJSONFile != null) {
                ButtonParse.IsEnabled = true;

                // temporary
                TextBoxInputJsonFilePath.Text = currentJSONFile.Path;
                var jsonStr = await FileIO.ReadTextAsync(currentJSONFile);
                TextBoxInputJSON.Text = jsonStr;

            } else {
                ButtonParse.IsEnabled = false;

                // temporary
                TextBoxInputJsonFilePath.Text = "";
                TextBoxInputJSON.Text = "";

            }
        }

        private async void ButtonParse_Click(object sender, RoutedEventArgs e)
        {
            if (currentJSONFile != null) {
                
                // temporary
                await demoDriver.RunTestParse(currentJSONFile);

            }
        }

        private async Task<StorageFile> ChooseInputFile(string settingsIdentifier, PickerLocationId suggestedStartLocation)
        {
            StorageFile file = null;
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = suggestedStartLocation;
            openPicker.FileTypeFilter.Add(".json");
            // openPicker.FileTypeFilter.Add(".txt");
            openPicker.SettingsIdentifier = settingsIdentifier;
#if WINDOWS_PHONE_APP
                openPicker.PickSingleFileAndContinue();
                // TBD: Picked file should be handled in OnActivated(IActivatedEventArgs args)
#else
            file = await openPicker.PickSingleFileAsync();
#endif
            return file;
        }

    }
}
